﻿using System;
using System.Windows;
using Autofac;
using Columbus;
using Microsoft.Phone.Controls;

namespace SimpleMultiFrameApplication
{
    public class ApplicationModule : Module
    {
        protected override void  Load(ContainerBuilder builder)
        {
            // Registering all views and controllers in current assembly
            builder
                .RegisterAssemblyTypes(GetType().Assembly)
                .Where(t => typeof(PhoneApplicationPage).IsAssignableFrom(t))
                .Named<FrameworkElement>(t => t.Name);
            builder
                .RegisterAssemblyTypes(GetType().Assembly)
                .Where(t => typeof(Controller).IsAssignableFrom(t))
                .Named<Controller>(t => t.Name);

            // Navigation
            builder.Register<Func<string, Controller>>(c => x => c.ResolveNamed<Controller>(x));
            builder.Register<Func<string, FrameworkElement>>(c => x => c.ResolveNamed<FrameworkElement>(x));
            builder.RegisterType<TransientStore>().As<ITransientStore>().InstancePerLifetimeScope();
            builder.RegisterType<NavigationAppication>().InstancePerLifetimeScope()
                .WithParameter("defaultAction", new Action<INavigator>(n => n.NavigateTo<ApplicationController>(c => c.Page1())));
            builder.RegisterType<Navigator>().As<INavigator>().InstancePerLifetimeScope()
                .WithParameter("frameName", "frame1");
            builder.RegisterType<Navigator>().As<INavigator>().InstancePerLifetimeScope()
                .WithParameter("frameName", "frame2");
        }
    }
}